package com.td.erhome.ui.login;

import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.td.erhome.R;
import com.td.erhome.base.BaseActivity;
import com.td.erhome.models.BaseBean;
import com.td.erhome.models.User;
import com.td.erhome.net.Api;
import com.td.erhome.net.BaseSubscriber;
import com.td.erhome.ui.home.HomeActivity;
import com.td.erhome.utils.ConstUtils;
import com.td.erhome.utils.RxBus;
import com.td.erhome.utils.T;
import com.td.erhome.utils.TextUtil;
import com.td.erhome.utils.UserManager;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class BindPhoneActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.tv_get_code)
    TextView mTvGetCode;
    @Bind(R.id.et_name)
    EditText mEtName;
    @Bind(R.id.tv_password)
    EditText mTvPassword;
    @Bind(R.id.tv_sure)
    TextView mTvSure;
    private String mOpen_id;
    private CountDownTimer mTimer;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bind_phone;
    }

    @Override
    protected void initView() {
        mOpen_id = getIntent().getStringExtra("open_id");
    }

    @Override
    protected void releaseResource() {
        if (mTimer!=null){
            mTimer.cancel();
        }

    }

    public void getData() {

        HashMap<String, Object> params = new HashMap<>();
        params.put("openid", mOpen_id);
        params.put("captcha", mTvPassword.getText().toString());
        params.put("mobile",  mEtName.getText().toString());
        Api.getRetrofit().bindAccountInfo(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseBean<User>>(BindPhoneActivity.this){
                    @Override
                    public void onNext(BaseBean<User> userBaseBean) {
                        super.onNext(userBaseBean);
                        UserManager.getIns().saveUserInfo(userBaseBean.getData()); //保存用户信息
                        startActivity(new Intent(BindPhoneActivity.this, HomeActivity.class));
                        RxBus.getInstance().post(ConstUtils.LOGOUT);
                        finish();
                    }
                });

    }

    public void getCode(){

        HashMap<String, Object> params = new HashMap<>();
        params.put("mobile",  mEtName.getText().toString());
        params.put("client_id", "t7vgeehvhcg40sks");
        Api.getRetrofit().sendLoginCaptcha(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseBean>(BindPhoneActivity.this){
                    @Override
                    public void onNext(BaseBean baseBean) {
                        super.onNext(baseBean);
                        mTvGetCode.setClickable(false);
                        mTimer.start();
                    }
                });
    }



    @OnClick({R.id.back, R.id.tv_get_code, R.id.tv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_get_code:
                if (TextUtil.isPhone(mEtName.getText().toString())){
                    mTimer = new CountDownTimer(60000,1000){
                        @Override
                        public void onTick(long millisUntilFinished) {
                            mTvGetCode.setText(millisUntilFinished/1000+"s");
                        }
                        @Override
                        public void onFinish() {
                            mTvGetCode.setClickable(true);
                            mTvGetCode.setText("重新获取");
                        }
                    };
                    getCode();
                }
                else {
                    T.showLong(this,"请输入正确手机号码");
                }
                break;
            case R.id.tv_sure:
                if (TextUtil.isEditTextEmpty(mEtName)){
                    T.showLong(this,"请输入手机号码");
                    return;
                }

                if (TextUtil.isEditTextEmpty(mTvPassword)){
                    T.showLong(this,"请输入验证码");
                    return;
                }

                getData();

                break;
        }
    }


}
